package com.algorithm.hqy;

import java.lang.reflect.Array;

/**
 * @Classname Stack
 * @Date 2019/9/18 15:40
 * @auther by hqy
 */
public class ArrayStack<T> {
    private int maxSize;

    private T[] array;

    private int top = -1;


    public ArrayStack(int maxSize, Class<T> type) {
        this.maxSize = maxSize;
        array = (T[]) Array.newInstance(type, maxSize);
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    public void push(T num) {
        if (isFull()) {
            System.out.println("栈已经满了~~~~~");
            return;
        }
        top++;
        array[top] = num;

    }

    public T pop() {
        if (isEmpty()) {
            throw new RuntimeException("栈已经空了~~~~~");
        }
        T value = array[top];
        top--;
        return value;
    }

    public T peek() {
        if (isEmpty()) {
            throw new RuntimeException("栈已经空了~~~~~");
        }
        return array[top];
    }

    public void show() {
        if (isEmpty()) {
            System.out.println("栈已经空了~~~~~");
            return;
        }
        for (int i = top; i >= 0; i++) {
            System.out.printf("stack[%d]=%d \r", i, array[i]);
        }

    }

}
